using BitMango;
using BMCommon;
using Holoville.HOTween;
using UnityEngine;

public class GUISceneFader : SingletonMonoBehaviour<GUISceneFader>
{
    public Color fadeColor = new Color(0f, 0f, 0f, 0.3f);

    private GUIStyle backgroundStyle;

    private void Start()
    {
        backgroundStyle = new GUIStyle();
        backgroundStyle.normal.background = BMTool.CreateDummyTexture(32, 32, Color.black);
    }

    public static void ValidateInstance()
    {
        if (SingletonMonoBehaviour<GUISceneFader>.Instance == null)
        {
            GameObjectExt.CreateTemporaryGameObjectWithComponent<GUISceneFader>();
        }
    }

    public static void FadeIn(float sec)
    {
        ValidateInstance();
        HOTween.To(SingletonMonoBehaviour<GUISceneFader>.Instance, sec, "fadeColor", new Color(0f, 0f, 0f, 0f));
    }

    public static void FadeOut(float sec, float alpha)
    {
        ValidateInstance();
        HOTween.To(SingletonMonoBehaviour<GUISceneFader>.Instance, sec, "fadeColor", new Color(0f, 0f, 0f, alpha));
    }

    private void OnGUI()
    {
        if (fadeColor.a > 0f)
        {
            GUI.color = fadeColor;
            GUI.depth = -1000;
            GUI.Label(new Rect(-10f, -10f, Screen.width + 10, Screen.height + 10), (Texture)null, backgroundStyle);
        }
    }
}
